﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace Eniux.Windows.EnterpriseLibrary.Commands
{
    /// <summary>
    /// 命令触发器基类
    /// </summary>
    public abstract class CommandTrigger : Freezable, ICommandTrigger
    {
        #region 属性

        public bool IsInitialized { get; set; } 

        #endregion

        #region 依赖属性

        #region Command

        /// <summary>
        /// Command属性
        /// </summary>
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTrigger),
            new FrameworkPropertyMetadata(null));

        /// <summary>
        /// 获取或设置Command的值
        /// </summary>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        #endregion

        #region CommandParameter

        /// <summary>
        /// CommandParameter属性
        /// </summary>
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(CommandTrigger),
            new FrameworkPropertyMetadata(null));

        /// <summary>
        /// 获取或设置CommandParameter属性的值
        /// </summary>
        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        #endregion

        #region CommandTarget

        /// <summary>
        /// CommandTarget属性
        /// </summary>
        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTrigger),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

        /// <summary>
        /// 命令目标属性
        /// </summary>
        public IInputElement CommandTarget
        {
            get { return (IInputElement)GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        #endregion

        #endregion

        #region 方法

        void ICommandTrigger.Initialize(FrameworkElement source)
        {
            if (IsInitialized)
                return;

            InitializeCore(source);
            IsInitialized = true;
        }

        protected abstract void InitializeCore(FrameworkElement source);

        protected void ExecuteCommand(CommandParameter<object> parameter)
        {
            if (Command == null)
                return;

            RoutedCommand routedCommand = Command as RoutedCommand;
            if (routedCommand != null)
            {
                routedCommand.Execute(parameter, CommandTarget);
            }
            else
            {
                Command.Execute(parameter);
            }
        }

        #endregion
    }
}
